package fr.obsmip.sedoo.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import fr.obsmip.sedoo.client.PortailRBV;
import fr.obsmip.sedoo.client.event.UserLoginEvent;
import fr.obsmip.sedoo.client.event.UserLogoutEvent;
import fr.obsmip.sedoo.client.mvp.Presenter;
import fr.obsmip.sedoo.shared.domain.RBVUserDTO;
import fr.sedoo.commons.client.language.place.LanguageSwitchPlace;
import fr.sedoo.commons.client.message.CommonMessages;
import fr.sedoo.commons.client.mvp.place.LoginPlace;
import fr.sedoo.commons.client.util.LocaleUtil;

public class HeaderViewImpl extends Composite implements HeaderView {

	private Presenter presenter;
	private static HeaderViewImplUiBinder uiBinder = GWT
			.create(HeaderViewImplUiBinder.class);

	interface HeaderViewImplUiBinder extends UiBinder<Widget, HeaderViewImpl> {
	}

	
	@UiField Image englishLanguageImage;
	@UiField Image frenchLanguageImage;
	 @UiField Anchor signOutLink;
	 @UiField Anchor signInLink;
	 @UiField Element userName;
	 @UiField Label notConnectedMessage;
	 
	  public HeaderViewImpl() {
	    initWidget(uiBinder.createAndBindUi(this));
	    if (PortailRBV.getClientFactory().getUserManager().getUser()!= null)
	    {
	    	signOutLink.setVisible(true);
	    	signInLink.setVisible(false);
	    }
	  }

//	  @UiHandler("welcomeLink")
//	  void onWelcomeClicked(ClickEvent event) {
//		  
//		  getPresenter().goTo(new WelcomePlace());
//	  }
	  
	  @UiHandler("englishLanguageImage")
	  void onEnglishLanguageButtonClicked(ClickEvent event) {
		  getPresenter().goTo(new LanguageSwitchPlace(LocaleUtil.ENGLISH));
	  }
	  
	  @UiHandler("frenchLanguageImage")
	  void onFrenchLanguageButtonClicked(ClickEvent event) {
		  getPresenter().goTo(new LanguageSwitchPlace(LocaleUtil.FRENCH));
	  }
	  
	  @UiHandler("signInLink")
	  void onSignInClicked(ClickEvent event) {
		  Presenter presenter = PortailRBV.getPresenter();
		  presenter.goTo(new LoginPlace());
	  }
	  
	  @UiHandler("informationSiteLink")
	  void onInformationSiteLinkClicked(ClickEvent event) {
		  Window.open("http://rnbv.ipgp.fr/", "_blank", "");
	  }
	  
	  @UiHandler("collaborativeSiteLink")
	  void onCollaborativeSiteLinkClicked(ClickEvent event) {
		  Window.open("https://extra.core-cloud.net/projets/soere-rbv/", "_blank", "");
	  }
	  
	  
	  
	  @UiHandler("signOutLink")
	  void onSignOutClicked(ClickEvent event) {
		  PortailRBV.logout();
	  }
	  
	public Presenter getPresenter()
	{
		if (presenter == null)
		{
			presenter = PortailRBV.getPresenter();
		}
		return presenter;
	}

	@Override
	public void onNotification(UserLoginEvent event) 
	{
		RBVUserDTO user = (RBVUserDTO) PortailRBV.getClientFactory().getUserManager().getUser();
		notConnectedMessage.setVisible(false);
		userName.setInnerText(CommonMessages.INSTANCE.loggedAs()+" "+user.getName());
		signOutLink.setVisible(true);
    	signInLink.setVisible(false);
	}
	
	@Override
	public void onNotification(UserLogoutEvent event) 
	{
		notConnectedMessage.setVisible(true);
		userName.setInnerText("");
		signOutLink.setVisible(false);
    	signInLink.setVisible(true);
	}
	
}
